<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/016
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class BannerController extends Controller{

    /*
        列表
    */

    public function lists(){

        //$this->isAjaxLogin();//检查是否登录

        $page =I('post.page/d');
        $page_size = I('post.page_size/d');
        $statustype = I('post.statustype');

        $model = M('banner');

        $where = 1;
        if($statustype=='0'){
            $type = 'recommendrank';
            $where="statustype='0'";
            $count = $model->where($where)->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where($where)->order("recommendrank desc")->limit($limitStart,$page_size)->select();
        }
        if($statustype=='1'){
            $type = 'applyrank';
            $where="statustype='1'";
            $count = $model->where($where)->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where($where)->order("applyrank desc")->limit($limitStart,$page_size)->select();
        }
        if($statustype=='2'){
            $type = 'bankrank';
            $where="statustype='2'";
            $count = $model->where($where)->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where($where)->order("bankrank desc")->limit($limitStart,$page_size)->select();
        }
        if($statustype=='3'){
            $type = 'gamerank';
            $where="statustype='3'";
            $count = $model->where($where)->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where($where)->order("gamerank desc")->limit($limitStart,$page_size)->select();
        }
        if($statustype=='4'){
            $type = 'bankadverrank';
            $where="statustype='4'";
            $count = $model->where($where)->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where($where)->order("bankadverrank desc")->limit($limitStart,$page_size)->select();
        }

        if($statustype=='5'){
            $count = $model->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model->order('id desc')->limit($limitStart,$page_size)->select();
        }
            
        //print_r($result);die;
        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

	/*
     * 推荐banner列表
     */
    public function bannerlist(){

        //$this->isAjaxLogin();//检查是否登录

        $page =I('post.page/d');
        $page_size = I('post.page_size/d');

        $statustype=I('post.statustype');

        $model = M('banner');

        if($statustype=="0"){
            $count = $model->where('statustype="0"')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where('statustype="0"')->order('recommendrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="1"){
            $count = $model->where('statustype="1"')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where('statustype="1"')->order('applyrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="2"){
            $count = $model->where('statustype="2"')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where('statustype="2"')->order('bankrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="3"){
            $count = $model->where('statustype="3"')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where('statustype="3"')->order('gamerank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype=="4") {
            $count = $model->where('statustype="4"')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model->where('statustype="4"')->order('bankadverrank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype==null){
            $count = $model->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model->order('id desc')->limit($limitStart,$page_size)->select();
        }


        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    
      /*修改前渲染*/

    public function beforedit(){
        $id=I('post.id');
        $model=M('banner');

        $data=$model->where('id='.$id)->find();
          if($data !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['data'] = $data;
            $ajaxReturn['msg']  = "SUCCESS";

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "数据查询失败，请稍后在试！";
        }

        $this->ajaxReturn($ajaxReturn);

    }
   

     /*修改*/
    public function save(){

        // $this->isAjaxLogin();//检查是否登录

        $Model = M('banner');

        $id     = I('post.id/d');
        $name   = I('post.name');
        $picture   =I('post.picture');
        $link_type  =I('post.link_type');
        $website    =I('post.website');
        $appsite    = I('post.appsite');
        $statustype    = I('post.statustype');
       

        $data['name']   = $name;
        $data['picture']   = $picture;
        $data['link_type']   = $link_type;
        $data['website']   = $website;
        $data['appsite']   = $appsite;
        $data['statustype']   = $statustype;
       

       

	    $result = $Model->where('id='.$id)->save($data);

	    if($result !== false){

	        $ajaxReturn['code'] = 1;
	        $ajaxReturn['msg']  = "SUCCESS";

	    }else{

	        $ajaxReturn['code'] = 2;
	        $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
	    }

       
      

        $this->ajaxReturn($ajaxReturn);
    }

     /*添加*/
    public function add(){

        //$this->isAjaxLogin();//检查是否登录

        $Model = M('banner');

        $name   = I('post.name');
        $picture   =I('post.picture');
        $link_type    = I('post.link_type');
        $website    =I('post.website');
        $appsite    = I('post.appsite');
        $statustype    = I('post.statustype');

       

        $data['name']   = $name;
        $data['picture']   = $picture;
        $data['link_type']   = $link_type;
        $data['website']   = $website;
        $data['appsite']   = $appsite;
        $data['statustype']   = $statustype;


        $arr  = $Model->select();

        if($arr==null){
            $data['recommendrank'] = 1;
            $data['applyrank'] = 1;
            $data['bankrank'] = 1;
            $data['gamerank'] = 1;
        }

        foreach($arr as $v){
            $data['recommendrank'] = $v['id']+1;
            $data['applyrank'] = $v['id']+1;
            $data['bankrank'] = $v['id']+1;
            $data['gamerank'] = $v['id']+1;
        }

       
       

        $result = $Model->add($data);

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
        }

       
      

        $this->ajaxReturn($ajaxReturn);
    }

     /*
     * 删除
     */
    public function delete (){

        $ids = I('post.id');

        $model = M('banner');


        $ids = explode(',',$ids);

        foreach($ids as $id){
            $model-> where('id='.$id)->delete();
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }



    /*
     * 推荐banner排序
     */
    public function bannerrank (){

        $statustype=I('post.statustype');

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('banner');


        if($statustype=="0"){
            $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('recommendrank,name');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('recommendrank'.$and.$thisrank)->order('recommendrank '.$order)->getField('id,recommendrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];

            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('id='.$thisid)->setField('recommendrank',$nextrank);
                $ArticleModel-> where('id='.$nextid)->setField('recommendrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }elseif ($statustype=="1") {
            $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('applyrank,name');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('applyrank'.$and.$thisrank)->order('applyrank '.$order)->getField('id,applyrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];

            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('id='.$thisid)->setField('applyrank',$nextrank);
                $ArticleModel-> where('id='.$nextid)->setField('applyrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }elseif ($statustype=="2") {
                $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('bankrank,name');
                $thisrank   = array_keys($thisinfo)[0];
                $thiscid    = array_values($thisinfo)[0];


                $nextinfo   = $ArticleModel->where('bankrank'.$and.$thisrank)->order('bankrank '.$order)->getField('id,bankrank');
                $nextid     = array_keys($nextinfo)[0];
                $nextrank   = array_values($nextinfo)[0];

                if($thisrank && $nextid && $nextrank){

                    $ArticleModel-> where('id='.$thisid)->setField('bankrank',$nextrank);
                    $ArticleModel-> where('id='.$nextid)->setField('bankrank',$thisrank);

                    $data['thisid'] = $thisid;
                    $data['thisrank'] = $thisrank;
                    $data['nextid'] = $nextid;
                    $data['nextrank'] = $nextrank;

                    $ajaxReturn['code'] = 1;
                    $ajaxReturn['msg'] = "SUCCESS";
                    $ajaxReturn['data'] = $data;
                }else{

                    $ajaxReturn['code'] = 2;
                    $ajaxReturn['msg'] = "数据更新失败！";
                }
        }elseif ($statustype=="3") {
                $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('gamerank,name');
                $thisrank   = array_keys($thisinfo)[0];
                $thiscid    = array_values($thisinfo)[0];


                $nextinfo   = $ArticleModel->where('gamerank'.$and.$thisrank)->order('gamerank '.$order)->getField('id,gamerank');
                $nextid     = array_keys($nextinfo)[0];
                $nextrank   = array_values($nextinfo)[0];

                if($thisrank && $nextid && $nextrank){

                    $ArticleModel-> where('id='.$thisid)->setField('gamerank',$nextrank);
                    $ArticleModel-> where('id='.$nextid)->setField('gamerank',$thisrank);

                    $data['thisid'] = $thisid;
                    $data['thisrank'] = $thisrank;
                    $data['nextid'] = $nextid;
                    $data['nextrank'] = $nextrank;

                    $ajaxReturn['code'] = 1;
                    $ajaxReturn['msg'] = "SUCCESS";
                    $ajaxReturn['data'] = $data;
                }else{

                    $ajaxReturn['code'] = 2;
                    $ajaxReturn['msg'] = "数据更新失败！";
                }
        }elseif($statustype=="4"){
                $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('bankadverrank,name');
                $thisrank   = array_keys($thisinfo)[0];
                $thiscid    = array_values($thisinfo)[0];


                $nextinfo   = $ArticleModel->where('bankadverrank'.$and.$thisrank)->order('bankadverrank '.$order)->getField('id,bankadverrank');
                $nextid     = array_keys($nextinfo)[0];
                $nextrank   = array_values($nextinfo)[0];

                if($thisrank && $nextid && $nextrank){

                    $ArticleModel-> where('id='.$thisid)->setField('bankadverrank',$nextrank);
                    $ArticleModel-> where('id='.$nextid)->setField('bankadverrank',$thisrank);

                    $data['thisid'] = $thisid;
                    $data['thisrank'] = $thisrank;
                    $data['nextid'] = $nextid;
                    $data['nextrank'] = $nextrank;

                    $ajaxReturn['code'] = 1;
                    $ajaxReturn['msg'] = "SUCCESS";
                    $ajaxReturn['data'] = $data;
                }else{

                    $ajaxReturn['code'] = 2;
                    $ajaxReturn['msg'] = "数据更新失败！";
                }
        }

        

        $this->ajaxReturn($ajaxReturn);

    }

   

    /*app搜索*/

    public function appsearch(){
        $appname=I('post.name');
        $model=M('gameinfo');

        if($appname==null){
            $data = '';
        }

        $data=$model->field('id,name')->where("name LIKE '%$appname%'")->select();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
        
        $this->ajaxReturn($ajaxReturn);

    }

      /*
    详情
    */
    public  function infos(){
        //$this->isAjaxLogin();
        $result = array
                      (
                    array(
                      'id'=>'0','name'=>'推荐banner'
                      ),
                    array('id'=>'1','name'=>'应用banner'
                    ),
                    array(
                        'id'=>'2','name'=>'金融banner'
                    ),
                    array(
                        'id'=>'3','name'=>'游戏banner'
                    ),
                    array(
                        'id'=>'4','name'=>'金融广告位'
                    )
                      
                      );

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }
}